<!--{include index/header}-->
<script type="text/javascript">
	$(function(){
		$('#city').change(function(){
			var self = this;
			var province = $('#province');
			var locale = $('#locale');
			province.html('');
			locale.html('');
			if(!$(self).val()) return;
			$.ajax({
				type:'POST',
				url:'{$DOMAIN_ROOT}index.php/member/getCityChildren',
				dataType:'json',
				data:'cityid='+$(self).val(),
				success:function(data){
					if(!data.length) return;
					var tmp = '<option >==请选择==</option>';
					for(var i in data){
						var city = data[i];
						tmp += '<option value="'+city.id+'">'+city.name+'</option>';
					}
					province.html(tmp);					
				}
			});
		});
		$('#province').change(function(){
			var self = this;
			var locale = $('#locale');
			locale.html('');
			if(!$(self).val()) return;
			$.ajax({
				type:'POST',
				url:'{$DOMAIN_ROOT}index.php/member/getCityChildren',
				dataType:'json',
				data:'cityid='+$(self).val(),
				success:function(data){
					if(!data.length) return;
					var tmp = '<option >==请选择==</option>';
					for(var i in data){
						var city = data[i];
						tmp += '<option value="'+city.id+'">'+city.name+'</option>';
					}
					locale.html(tmp);					
				}
			});
		});
		$('#address-form').submit(function(){
			$.ajax({
				type:'POST',
				url:'{$DOMAIN_ROOT}index.php/member/address',
				data:$(this).serialize(),
				dataType:'html',
				success:function(res){
					if(res == '1'){
						right('成功了！您的新收货地址添加成功了！',function(){
						this.location.href="{$DOMAIN_ROOT}index.php/member/address";
					});
					}else if (res == '2') {
						right('成功了！您的收货地址修改成功了！',function(){
						this.location.href="{$DOMAIN_ROOT}index.php/member/address";
						});
					}else if (res == '-1') {
							wrong('出错了！您的已保存的有效地址已达到系统规定的数！');
						} 
				}				
			});
		});	
		$('#addressSaved .thead-tbl-address').hover(function(){
			$(this).attr('bgcolor','#e1ebf7');
			var stats= $(this).find('.thead-tbl-status');
			if(stats.html() == '默认地址') return;
			stats.html('<a href="javascript:;" class="setDefault">设为默认</a>');
		},function(){
			$(this).attr('bgcolor','white');
			var stats= $(this).find('.thead-tbl-status');
			if(stats.html() == '默认地址') return;
			stats.html('');
		});
	
		$('#addressSaved .setDefault').live('click', function() {
			var line = $(this).closest('tr');
			if(!line.attr('rid')) return;
			$.ajax({
				type:'POST',
				url:'{$DOMAIN_ROOT}index.php/member/setDefaultAddress',
				dataType:'html',
				data:'id='+line.attr('rid'),
				success:function(data){
					if(data == '1') right('成功了！您的默认收货地址设置成功!',function(){
						this.location.href="{$DOMAIN_ROOT}/index.php/member/address";
					});			
				}
			});
			return false;
		});
		$('#addressSaved .edit').click(function(){
			var line = $(this).closest('tr');
			var realname = $('.add_realname',line);
			var zipcode = $('.add_zip_code',line);
			var location = $('.add_location',line);
			if(location != ''){
				location = location.text().split(' ');
				city_name = location[0];
				province_name = location[1];
				street_name = location[2];
				$('#city option').each(function(i){
					var self = $(this);
					if(self.html() == city_name){
						self.attr('selected',true);
						$.post('{$DOMAIN_ROOT}index.php/member/getCityChildren','cityid='+self.val(),function(data){
							if(!data.length) return;
							var tmp = '<option>==请选择==</option>';
							for(var i in data){
								if(data[i].name == province_name){
									tmp += '<option value="'+data[i].id+'" selected>'+data[i].name+'</option>';
									$.post('{$DOMAIN_ROOT}index.php/member/getCityChildren','cityid='+data[i].id,function(street_data){
										if(!street_data.length) return;
										var tmp_street = '<option>==请选择==</option>';
										for( var j in street_data)
										{
											if(street_data[j].name == street_name){
												tmp_street+='<option value="'+street_data[j].id+'" selected>'+street_data[j].name+'</option>';
											}else{
												tmp_street += '<option value="'+street_data[j].id+'">'+street_data[j].name+'</option>';
											}
										}
										$('#locale').html(tmp_street);
									},'json');
								}else{
									tmp += '<option value="'+data[i].id+'">'+data[i].name+'</option>';
								}
							}
							$('#province').html(tmp);
						},'json');
					}
				})
			}
			var street  = $('.add_street',line);
			var mobile  = $('.add_mobile',line);
			var isdefault = $('.add_default',line);
			if(realname) $('#address-username').val(realname.text());
			if(zipcode) $('#address_zipcode').val(zipcode.text());
			if(street) $('#address_street').val(street.text());
			if(mobile) $('#address_mobile').val(mobile.text());
			if(isdefault.text()=='默认地址'){
				$('#address_default').attr('checked',true);	
			}else{
				$('#address_default').attr('checked',false);	
			}
			$('#addressid').val(line.attr('rid'));
			return false;			
		});
		$('#addressSaved .delete').click(function(){
			var line = $(this).closest('tr');
			var addressid = $('#addressid');
			if(addressid.val()) addressid.val('');
			if(!line.attr('rid')) return;
			$.ajax({
				type:'POST',
				url:'{$DOMAIN_ROOT}index.php/member/delAddress',
				dataType:'html',
				data:'id='+line.attr('rid'),
				success:function(data){
					if(data == '1'){
						line.remove();
					}
				}
			});
		});	
	});
</script>
<div id="bdw" class="bdw">
<div id="bd" class="cf">
<div id="settings">
	<!--{include member/menu}-->
    <div id="content" class="clear settings-box">
		<div class="box clear">
            <div class="box-top"></div>
            <div class="box-content">
                <div class="head"><h2>收货地址</h2></div>
                <div class="sect" style="padding-top:0px;">
                    <form id="address-form" method="post" action="javascript:;" class="validator">
                    	<input type="hidden" name="uid" value="{$uid}"/>
						<input type="hidden" name="address_id" value="" id="addressid"/>
						<div class="wholetip clear" style="padding-left:100px;"><span style="color:#FC6210;font-weight:bold;">新增收货</span>地址电话号码、手机号选填一项,其余均为必填项</div>
                        <div class="field username">
                            <label>收货人姓名</label>
                            <input name="username" id="address-username" class="f-input" value="" require="true" datatype="limit" min="2" max="16" maxlength="16" type="text">
						</div>
                        <div class="field password">
                            <label>所在地区</label>
                            <select name="city" id="city">
                            	<option value="">==请选择==</option>
                            	<!--{loop $citys $index $one}-->
								<option value="{$index}">{$one}</option>
								<!--{/loop}-->
                            </select>省
                            <select name="province" id="province">
                            	<option value="">==请选择==</option>
                            </select>市
                            <select name="locale" id="locale">
                            	<option value="">==请选择==</option>
                            </select>区
                        </div>
                        <div class="field password">
                            <label>街道地址</label>
                            <input size="30" name="street_address" id="address_street" class="f-input" type="text">
							<span class="hint">不需要重复填写省/市/区</span>
                        </div>
						<div class="field">
                            <label>邮政编码</label>
                            <input maxlength="6" size="10" name="zipcode" id="address_zipcode" class="f-input" style="width:87px;" value="" type="text">
                        </div>
                        <div class="field mobile">
                            <label>手机号</label>
                            <input size="30" name="mobile" id="address_mobile" class="f-input" style="width:87px;" value="" type="text"><span class="inputtip">手机号码是我们联系你的最重要的联系方式</span>
                        </div>
                        <div class="field">
                            <label>设为默认</label>
                            <input type="checkbox" name="isdefault" value="1" id="address_default" style="margin-top:5px;"/>
                        </div>
                        <div class="clear"></div>
                        <div class="act">
                            <input value="保存" name="commit" id="settings-submit" class="formbutton" type="submit">
                        </div>
                    </form>
					<div>
						<style type="text/css">
							#addressSaved{border:1px solid #A2CBFF;width:100%;table-layout:fixed;color:#404040;font-size:13px;}
							#addressSaved th{border-top:1px solid #A2CBFF;border-bottom:1px solid #AEC7E5;height:29px;text-align:center;background:url("http://a.tbcdn.cn/app/uc/img/bg_thead_grade.png") repeat-x scroll 0 0 transparent;}
							#addressSaved td{border-bottom:1px solid #A2CBFF;padding:5px;white-space:normal;word-wrap:break-word;}
							#addressSaved .cell-align-center{text-align:center;}
							#addressSaved caption{color:#FC6210;font-weight:700;height:21px;}
						</style>
						<table id="addressSaved">
							<caption>已保存的有效地址</caption>
							<tbody>
							<tr>
								<th>收货人</th>
								<th>所在地区</th>
								<th>街道地址</th>
								<th>邮编</th>
								<th>手机</th>
								<th>&nbsp;</th>
								<th>操作</th>
							</tr>
							<!--{loop $addressSaved $index $one}-->
							<tr class="thead-tbl-address" rid="{$one['id']}">
								<td class="cell-align-center add_realname">{$one['realname']}</td>
								<td class="add_location">{$one['location']}</td>
								<td class="add_street">{$one['street']}</td>
								<td class="cell-align-center add_zip_code">{$one['code']}</td>
								<td width="120" class="add_mobile">{$one['mobile']}</td>
								<td class="thead-tbl-status cell-align-center add_default" status="{$one['default']}"><!--{if $one['isdefault']}-->默认地址<!--{/if}--></td>
								<td class="cell-align-center"><a href="javascript:;" class="edit">修改</a>&nbsp;|&nbsp;<a href="javascript:;" class="delete">删除</a></td>
							</tr>
							<!--{/loop}-->
							</tbody>
						</table>
						<span class="hint">最多保存5个有效地址</span>
					</div>
                </div>
            </div>
            <div class="box-bottom"></div>
        </div>
    </div>
    <!--{include member/credit_sidebar}-->
</div>
</div> <!-- bd end -->
</div> <!-- bdw end -->
<!--{include index/footer}-->